package com.example.cathouse;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.tabs.TabLayout;

import java.util.Objects;

public class ScienceFragment extends Fragment {

    private ScienceViewModel scienceViewModel;
    private Toast mToast = null;
    private com.example.cathouse.ui2.feed.feedFragment feedFragment;
    private com.example.cathouse.ui2.disease.diseaseFragment diseaseFragment;
    private com.example.cathouse.ui2.adoption.adoptionFragment adoptionFragment;
    private com.example.cathouse.ui2.ligation.ligationFragment ligationFragment;
    private com.example.cathouse.ui2.touch_cats.touch_catsFragment touch_catsFragment;

    public static ScienceFragment newInstance() {
        return new ScienceFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        scienceViewModel =
                new ViewModelProvider(this).get(ScienceViewModel.class);
        View view = inflater.inflate(R.layout.science_fragment, container, false);
        final TextView textView = view.findViewById(R.id.text_dashboard);

        feedFragment = feedFragment.newInstance();
        diseaseFragment = diseaseFragment.newInstance();
        adoptionFragment = adoptionFragment.newInstance();
        ligationFragment = ligationFragment.newInstance();
        touch_catsFragment = touch_catsFragment.newInstance();
        getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1,feedFragment).commit();

        TabLayout tabLayout1 = (TabLayout) view.findViewById(R.id.tablayout1);
        tabLayout1.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onTabSelected(TabLayout.Tab tab1) {


                    //判断队列中是否包含已经显示的Toast
                    if (mToast == null) {
                        mToast = Toast.makeText(getActivity(), tab1.getText(), Toast.LENGTH_SHORT);
                    }
                    else{
                        mToast.cancel();
                        mToast = Toast.makeText(getActivity(), tab1.getText(), Toast.LENGTH_SHORT);
                        mToast.setText(tab1.getText());
                    }
                    mToast.show();


                //Toast.makeText(getActivity(),tab1.getText(),Toast.LENGTH_SHORT).show();
                if (tab1.getText().equals("喂食")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, feedFragment).commit();
                }
                if (tab1.getText().equals("疾病")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, diseaseFragment).commit();
                }
                if (tab1.getText().equals("领养")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, adoptionFragment).commit();
                }
                if (tab1.getText().equals("绝育")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, ligationFragment).commit();
                }
                if (tab1.getText().equals("撸猫")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, touch_catsFragment).commit();

                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        scienceViewModel = new ViewModelProvider(this).get(ScienceViewModel.class);
        Objects.requireNonNull(((AppCompatActivity) requireActivity()).getSupportActionBar()).hide();//隐藏掉整个ActionBar
    }
}